<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/29
 * Time: 16:12
 */
set_time_limit(0);
ini_set('memory_limit', '1024M');
require_once 'Excel/reader.php';
require 'qiniu/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
class index
{
    const CODE = 'code/code.json';
    const GOODS_PATH = 'goods/';
    const ALL = 0;
    const APPEND = 8;
    const accessKey = 'ZV4O7ZT0ZINIPrVXOtpnLC_hFFBqSTOk50_BmAvU';
    const secretKey = '89jw0XoQKRX3kISV3MGqNU-0lT4-tGGiNGNZ_VI7';
    private $file;

    public function __construct($file='')
    {
        $this->file = $file;
    }

    public function getTk()
    {
        $data = $this->getGoods();
//        foreach ($data as $key => $val) {
//            $file_name = self::GOODS_PATH . $key . '.json';
//            file_put_contents($file_name, json_encode($val, true), self::ALL);
//        }
    }

    private function getGoods()
    {
        $file = $this->file;
        $sheet = 0;
        $execl = new Spreadsheet_Excel_Reader();
        $execl->setOutputEncoding('UTF-8');
        $execl->read($file);
        $insert = [];
        $code = [];
        $map = $execl->sheets[$sheet]['cells'];
        array_shift($map);
        foreach ($map as $key=>$row){
            $row = array_values($row);
            $ckey = md5($row[4]);
            $code[$ckey] = $row[4];
            $end_time = strtotime($row[19]);
            if($end_time < time()){
                continue;
            }
            $temp=[
                'id' => $row[0],
                'name' => $row[1],
                'img' => $row[2],
                'url' => $row[3],
                'tk_url' => $row[5],
                'type' => $row[4],
                'price' => $row[6],
                'salf_num' => $row[7],
                'yongBi' => $row[8],
                'yongPrice' => $row[9],
                'seller_id' => $row[11],
                'seller' => $row[12],
                'tao' => $row[13],
                'youNum' => $row[15],
                'youSheng' => $row[16],
                'youMain' => $row[17],
                'youStart' => $row[18],
                'youEnd' => $row[19],
                'cn_url' => $row[20],
                'cn_tk_url' => $row[21]
            ];
            $insert[$ckey][] = $temp;
//            $this->uploadQiNiu($row[2],$row[0].'.jpg');
            $file_name = self::GOODS_PATH . $ckey . '.json';
            file_put_contents($file_name, json_encode($temp, true).',', self::APPEND);
        }
       
        $codes = file_get_contents(self::CODE);
        if ($codes) {
            $code = array_merge($code, json_decode($codes, true));
        }
        file_put_contents(self::CODE, json_encode($code, true),0);
        return $insert;
    }

    private function uploadQiNiu($filePath='',$img_name=''){
       if(!$filePath || !$img_name) return false;
        $auth = new Auth(self::accessKey, self::secretKey);
        $bucket = 'taobaoke';
        $bmgr = new BucketManager($auth);
        list($ret, $err) = $bmgr->fetch($filePath, $bucket, $img_name);
        if ($err !== null) {
        	return false;
        } else {
        	return  true;
        }
    }
}

list($f,$arg)=$argv;
$app = new index($arg);
$app->getTk();

